# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025/9/22 22:53
# @Author  : Dell
# @File    : evaluate.py
# @Software: PyCharm
# @Desc    :评估
from langchain_community.embeddings import DashScopeEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
from langchain.text_splitter import RecursiveCharacterTextSplitter
# 模型于文档加载
from langchain_community.document_loaders import TextLoader
#评估链
from langchain.evaluation.qa import QAEvalChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model_name='qwen-plus', base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", temperature=0.5,
                 api_key="sk-7b4f290b1a3e42a1a9a1957fa44eff37")
loader = TextLoader(file_path="xiyouji.txt",encoding="utf-8")
documents = loader.load()

# 初始化分割器
text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=50)
# 文档切分
texts=text_splitter.split_documents(documents)

# 获取字符总数，以便我们稍后查看平均值
num_total_characters =sum([len(x.page_content) for x in texts])
print(f"现在你有{len(texts)}个文档，平均每个文档有{num_total_characters/len(texts)*100:.2f}% 个字符")

# 嵌入和文档存储
embeddings = DashScopeEmbeddings(model='text-embedding-v1',dashscope_api_key="sk-7b4f290b1a3e42a1a9a1957fa44eff37")
textsearch = FAISS.from_documents(texts,embeddings)

#检索
chain = RetrievalQA.from_chain_type(llm=llm,chain_type="stuff",retriever=textsearch.as_retriever(),input_key="question")
# 人工构建Q&A 类似监督学习：我们有个标准答案
question_answers=[
    {"question":"孙悟空的师傅是谁？","answer":"菩提祖师"},
    {"question":"孙悟空从哪里出生的？","answer":"石头里蹦出来的"}
]
predictions = chain.apply(question_answers)
print(predictions)
# 初始化评估量  借助llm评估
eval_chain = QAEvalChain.from_llm(llm)
# 让llm自己评分，下面的代码帮助eval_chain知道不同部分的位置
graded_outputs= eval_chain.evaluate(question_answers,predictions,question_key="question",prediction_key="result",answer_key="answer")
print(graded_outputs)